package yun.common.http.request;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import yun.common.http.response.IResponseFactory;

public abstract class Request {
	private String mEncoding;
	private List<BasicNameValuePair> mHeaders;
	private List<BasicNameValuePair> mParams;
	protected Request(String encoding) {
		mEncoding = encoding;
	}

	protected Request() {
		this(HTTP.UTF_8);
	}

	protected void addParams(String name, String value) {
		if (mParams == null) {
			mParams = new ArrayList<BasicNameValuePair>();
		}
		mParams.add(new BasicNameValuePair(name, value));
	}

	protected void addHeader(String name, String value) {
		if (mHeaders == null) {
			mHeaders = new ArrayList<BasicNameValuePair>();
		}
		mHeaders.add(new BasicNameValuePair(name, value));
	}

	public HttpPost getHttpPost() throws UnsupportedEncodingException, URISyntaxException, MalformedURLException {
		String encodedUrl = URI.create(getUrl()).toURL().toString();
		HttpPost post = new HttpPost(encodedUrl);
		if (mHeaders != null) {
			for (BasicNameValuePair header : mHeaders) {
				post.setHeader(header.getName(), header.getValue());
			}
		}

		if (mParams != null) {
			post.setEntity(new UrlEncodedFormEntity(mParams, mEncoding));
		}
		return post;

	}
	public HttpGet getHttpGet() throws UnsupportedEncodingException, URISyntaxException, MalformedURLException {
		StringBuilder url = new StringBuilder(getUrl());
		if (mParams != null) {
			url.append('?');
			BasicNameValuePair p;
			for (int i = 0; i < mParams.size(); i++) {
				if (i > 0) {
					url.append('&');
				}
				p = mParams.get(i);
				url.append(p.getName()).append('=').append(p.getValue());
			}
		}

		String encodedUrl = URI.create(url.toString()).toURL().toString();
		HttpGet get = new HttpGet(encodedUrl);
		if (mHeaders != null) {
			for (BasicNameValuePair header : mHeaders) {
				get.setHeader(header.getName(), header.getValue());
			}
		}
		return get;
	}
	public abstract String getUrl();
	@Override
	public String toString() {
		StringBuilder toString = new StringBuilder();
		toString.append("+--<" + getClass().getSimpleName() + ">---").append('\n');
		toString.append("| ").append("HTTP URL:").append(getUrl()).append('\n');
		toString.append("| ").append("Encoding:").append(mEncoding).append('\n');
		toString.append("|--headers").append('\n');
		if (mHeaders != null) {
			for (BasicNameValuePair h : mHeaders) {
				toString.append("| ").append("header").append('=').append(h.toString()).append('\n');
			}
		}
		toString.append("|--params").append('\n');
		if (mParams != null) {
			for (BasicNameValuePair p : mParams) {
				toString.append("| ").append("params").append('=').append(p.toString()).append('\n');
			}
		}
		toString.append("+--------------------").append('\n');
		return toString.toString();
	}
	public abstract IResponseFactory getResponseFactory();
}
